import base64
import json
import time

import requests
from lxml import etree


def login(s):
    url = 'http://passport2.chaoxing.com/fanyalogin'
    data = {'fid': '12007', 'uname': '110', 'password': '110',
            'refer': 'https://mooc1.chaoxing.com/exam/test/reVersionTestStartNew?keyboardDisplayRequiresUserAction=1',
            'courseId': '220031126', 'classId': '45167849', 'tId': '1462619', 'id': '43226321', 'p': '1', 'start': '0',
            'remainTimeParam': '7140', 'relationAnswerLastUpdateTime': '1634300092465',
            'enc': '56644d53af8f071ecbc6fb898c2c24fe', 'examsystem': '0', 'cpi': '151955181',
            'openc': '871d102226092d8a94273dd93c6733cf', 'qbanksystem': '0', 'qbankbackurl': '', 'monitorStatus': '0',
            'monitorOp': '-1', 't': 'true', 'forbidotherlogin': '0'}
    # s = requests.session()
    data['uname'] = input("用户名：")
    data['password'] = base64.b64encode(input("密码：").encode('utf-8'))
    r = s.post(url=url, data=data)
    print(r.text)
    status = json.loads(r.text)['status']
    # print(status)
    if status is True:
        print("login success")
        return s
    else:
        print("login fail")
        exit()


def getItems(s, num):
    #考试页面链接放这里，start为题号，它之前的放在url1，之后的放在url2
    url1 = 'http://mooc1.chaoxing.com/exam/test/reVersionTestStartNew?keyboardDisplayRequiresUserAction=1&courseId=220031126&classId=45167849&tId=1462619&id=43226321&p=1&start='
    url2 = '&remainTimeParam=3993&relationAnswerLastUpdateTime=1634303240516&enc=66e5518ca3933570a4802fbe06db3e8e&examsystem=0&cpi=151955181&openc=871d102226092d8a94273dd93c6733cf&qbanksystem=0&qbankbackurl=&monitorStatus=0&monitorOp=-1'
    url = url1 + str(num) + url2
    hdr = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive'}

    # r = requests.get(url, headers=hdr)
    r = s.get(url=url, headers=hdr)
    print(r)
    # print(r.text)
    html = etree.HTML(r.text)
    elem = html.xpath('//form/div/div')
    item = elem[0].text
    item = item.strip('\n')
    item = item.strip()
    item = item[:-6]
    print(item)
    return item


def search(item: str):
    api = 'http://huan.fm210.cn/api/answer?keyword='
    url = api + item
    headers = {
        'Host': 'huan.fm210.cn',
        'Connection': 'close',
        'Cache-Control': 'max-age=0',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Sec-Fetch-Site': 'none',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-User': '?1',
        'Sec-Fetch-Dest': 'document',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cookie': '__yjs_duid=1_84a73421d67c29d1e7a3c97cee0f272f1634301025457'
    }
    r = requests.get(url)
    answer = json.loads(r.text)['_source']['da']
    print(answer)
    return answer


def fuck():
    s = requests.session()
    s = login(s)
    # time.sleep(1)
    for i in range(85):
        item = getItems(s, i)
        time.sleep(1.6)
        with open('result.txt', mode='a+', encoding='utf-8') as f:
            answer = search(item)
            f.write(str(i+1) + '\t' + '\t' + item + '\t' + '\t' + answer + '\n')


if __name__ == '__main__':
    fuck()
